package com.fearless.crm.workbench.web.controller;

import com.fearless.crm.settings.entity.User;
import com.fearless.crm.utils.DateTimeUtil;
import com.fearless.crm.utils.UUIDUtil;
import com.fearless.crm.vo.ClueVoPagination;
import com.fearless.crm.workbench.entity.Activity;
import com.fearless.crm.workbench.entity.Clue;
import com.fearless.crm.workbench.entity.ClueRemark;
import com.fearless.crm.workbench.entity.Tran;
import com.fearless.crm.workbench.service.ActivityService;
import com.fearless.crm.workbench.service.ClueRemarkService;
import com.fearless.crm.workbench.service.ClueService;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("clue")
public class ClueController {

    @Resource
    private ClueService clueService;

    @Resource
    private ActivityService activityService;

    @Resource
    private ClueRemarkService clueRemarkService;


//查询所有者列表(用户列表),用于铺垫创建线索的模态窗口
    @RequestMapping("/getUserList.do")
    @ResponseBody
    Map<String,Object> getUserListController(){
        System.out.println("进入到getUserListController处理器方法");
        System.out.println("执行铺垫创建线索模态窗口的操作");
        List<User> userList=clueService.getUserListService();
        Map map=new HashMap();
        map.put("userlist",userList);

        return map;
    }

//执行线索的创建操作
    @RequestMapping("/saveClue.do")
    @ResponseBody
    public boolean saveClueController(HttpSession session,Clue clue){
        System.out.println("进入到saveClueController处理器方法");
        System.out.println("执行线索的创建操作");
        String id= UUIDUtil.getUUID();
        String createTime= DateTimeUtil.getSysTime();
        String createBy=((User)session.getAttribute("user")).getName();
        clue.setCreateBy(createBy);
        clue.setCreateTime(createTime);
        clue.setId(id);
        boolean flag=clueService.saveClueService(clue);
        return flag;
    }


//分页查询
    @RequestMapping("/pageList.do")
    @ResponseBody
    public ClueVoPagination pageListController(Clue clue,Integer pageNo,Integer pageSize){
        System.out.println("进入到pageListController处理器方法");
        System.out.println("执行线索的分页查询操作");
        System.out.println(clue.toString());
        System.out.println(pageNo+"    "+pageSize);

        //计算略过的页数
        Integer skipCount=(pageNo-1)*pageSize;

        Map map=new HashMap();
        map.put("fullname",clue.getFullname());
        map.put("company",clue.getCompany());
        map.put("phone",clue.getPhone());
        map.put("source",clue.getSource());
        map.put("owner",clue.getOwner());
        map.put("mphone",clue.getMphone());
        map.put("state",clue.getState());

        map.put("skipCount",skipCount);
        map.put("pageSize",pageSize);//limit(x,y) x代表略过几条记录，y代表展现多少条记录

        ClueVoPagination clueVoPagination =clueService.pageListService(map);

        //System.out.println("线索的总条数是"+total);
        return clueVoPagination;
    }

//跳转到线索详细信息页
    @RequestMapping("/clueDetail.do")
    @ResponseBody
    public ModelAndView clueDetailController(HttpServletRequest request,String id){
        System.out.println("进入到ModelAndViewController处理器方法");
        System.out.println("执行跳转到线索详细信息页");
        ModelAndView mv=new ModelAndView();
        Clue clue=clueService.getClueDetailService(id);
        request.setAttribute("clue",clue);
        mv.setViewName("forward:/workbench/clue/detail.jsp");
        return mv;
    }


//显示与线索关联的市场活动
    @RequestMapping("/showActivityListByClueId.do")
    @ResponseBody
    public List<Activity> showActivityListByClueIdController(String ClueId){
        //根据线索的id找到关联的市场活动列表，
        System.out.println("进入到showActivityListByClueIdController处理器方法");
        System.out.println("执行展现线索关联的市场活动列表的操作");
        System.out.println("线索的id"+ClueId);
        List<Activity> activityList=clueService.getActivityByClueIdService(ClueId);

        return activityList;
    }

//删除与线索关联的市场活动
    @RequestMapping("/unBundClueWithActivity.do")
    @ResponseBody
    public Map<String,Object> unBundClueWithActivityController(String id){
        System.out.println("进入到unBundClueWithActivityController处理器方法");
        System.out.println("执行解除线索关联的市场活动");

        Map map=clueService.unBundClueWithActivityService(id);

        return map;
    }


//获取到线索的相信信息，用于铺垫修改线索的模态窗口
    @RequestMapping("/editClue.do")
    @ResponseBody
    public Map<String,Object> editClueController(String id){
        System.out.println("进入到editClueController处理器方法");
        System.out.println("获取到线索信息用于铺垫修改线索的模态窗口");
        Map map=clueService.editClueService(id);

        return map;
    }


//根据名称模糊查询市场活动，并显示出来，用于关联市场活动操作
    @RequestMapping("/getActivityByNameAndNotConnectClue.do")
    @ResponseBody
    public List<Activity> getActivityByNameAndNotConnectClueController(String aname,String clueId){
        System.out.println("进入到getActivityByNameAndNotConnectClueController处理器方法");
        System.out.println("查询时候活动列表(根据名称模糊查询，并且排除掉已经和线索关联的市场活动)");

        Map map=new HashMap();
        map.put("aname",aname);
        map.put("clueId",clueId);

        List<Activity> activityList=activityService.getActivityByNameAndNotConnectClueService(map);
        return activityList;

    }

//执行关系表的添加操作，就是将线索和市场活动关联起来   【批量添加】
    @RequestMapping("/connect.do")
    @ResponseBody
    public Map connectController(String clueId,String[] activityId){
         System.out.println("进入到connectController处理器方法");
         System.out.println("执行线索关联市场活动的操作");

            System.out.println("收到的clueId"+clueId);
            for(String s:activityId){
            System.out.println("市场活动id是"+s);
            }


          boolean flag =clueService.connectService(clueId, activityId);

          Map map=new HashMap();
          map.put("success",flag);
        return map;
    }


//执行线索的修改操作，
    @RequestMapping("/updateClue.do")
    @ResponseBody
    public Map<String,Object> updateClueController(HttpSession session,Clue clue){
        System.out.println("进入到updateClueController处理器方法");
        System.out.println("执行线索的修改操作");
        String editBy=((User)session.getAttribute("user")).getName();
        String editTime=DateTimeUtil.getSysTime();
        clue.setEditBy(editBy);
        clue.setEditTime(editTime);
        boolean flag=clueService.updateClueService(clue);
        Map map=new HashMap();
        map.put("success",flag);
        return map;

    }

//根据线索的id来进行线索的删除操作
    @RequestMapping("/deleteClue.do")
    @ResponseBody
    public Map<String,Object> deleteClueController(String[] id){
        System.out.println("进入到deleteClueController处理器方法");
        System.out.println("执行线索的删除操作");
        //原理是通过复选框的value属性(里面存放的是线索的id,这是在分页查询动态拼接中完成的)
        //然后通过这个线索id执行批量/单个删除

        boolean flag=clueService.deleteClueByIdService(id);

        Map map=new HashMap();
        map.put("success",flag);

        return map;
    }




//根据名称模糊查询市场活动
    @RequestMapping("/queryActivityByName.do")
    @ResponseBody
    public List<Activity> queryActivityByNameController(String name){
        System.out.println("进入到queryActivityByNameController处理器方法");
        System.out.println("根据名称模糊查询市场活动");
        //System.out.println("接收到的参数"+name);
        List<Activity> activityList=activityService.queryActivityByNameService(name);
        return activityList;
    }





//显示线索相关的备注
    @RequestMapping("/clueRemark.do")
    @ResponseBody
    public Map<String ,Object> clueRemarkController(String id){
        System.out.println("进入到clueRemarkController处理器方法");
        System.out.println("执行线索备注的展示操作");
        //System.out.println("接受到的参数是"+id);


       Map map= clueRemarkService.showClueRemarkService(id);

        return map;
    }

//修改线索备注，用于铺垫线索备注的修改模态窗口
    @RequestMapping("/editClueRemark.do")
    @ResponseBody
    public Map<String,Object> editClueRemarkController(String id){
        System.out.println("进入到editClueRemarkController处理器方法");
        System.out.println("铺垫修改线索的模态窗口");

        //System.out.println("线索备注的id"+id);
        ClueRemark clueRemark=clueRemarkService.editClueRemarkService(id);
        Map map=new HashMap();
        map.put("clueRemark",clueRemark);
        return map;
    }

//执行修改线索备注的操作，
    @RequestMapping("/updateClueRemark.do")
    @ResponseBody
    public Map<String,Object> updateClueRemarkController(String noteContent,String id){
        System.out.println("进入到updateClueRemarkController处理器方法");
        System.out.println("执行线索备注修改操作");

        //System.out.println("接受到的参数"+noteContent+"   "+id);


        boolean flag=clueRemarkService.updateClueRemarkService(noteContent,id);

        Map map=new HashMap();
        map.put("success",flag);



        return map;
    }


//删除线索备注的操作,根据线索备注id删除
    @RequestMapping(value = "/deleteClueRemark.do",method = RequestMethod.POST)
    @ResponseBody
    public boolean deleteClueRemarkController(String id){
        System.out.println("进入到deleteClueRemarkController处理器方法");
        System.out.println("执行线索备注的删除操作");

        //System.out.println("接收到的参数"+id);
        boolean flag=clueRemarkService.deleteClueRemarkService(id);

        return flag;

    }


//新增线索备注的操作
    @RequestMapping("/saveClueRemark.do")
    @ResponseBody
    public boolean saveClueRemarkController(HttpSession session,String noteContent,String clueId){
        System.out.println("进入到saveClueRemarkController处理器方法");
        System.out.println("执行新增线索备注的操作");
        Map map=new HashMap();
        String id=UUIDUtil.getUUID();
        String createBy=((User)session.getAttribute("user")).getName();
        String createTime=DateTimeUtil.getSysTime();
        map.put("id",id);
        map.put("createBy",createBy);
        map.put("createTime",createTime);
        map.put("noteContent",noteContent);
        map.put("clueId",clueId);
        map.put("editFlag",0);

        boolean flag=clueRemarkService.saveClueRemarkService(map);


        return flag;
    }


//在详细信息页执行修改线索的操作
    @RequestMapping(value = "/editClueInDetail.do",method = RequestMethod.POST)
    @ResponseBody
    public boolean editClueInDetailController(HttpSession session,Clue clue){
        System.out.println("进入到editClueInDetailController处理器方法");
        System.out.println("执行在详细页修改线索的操作");

        String editTime=DateTimeUtil.getSysTime();
        String editBy=((User)session.getAttribute("user")).getName();

        clue.setEditTime(editTime);
        clue.setEditBy(editBy);
        boolean flag=clueService.editClueInDetailService(clue);



        return flag;
    }


//执行在详细页删除线索的操作
    @RequestMapping(value = "/deleteClueInDetail.do",method = RequestMethod.POST)
    @ResponseBody
    public boolean deleteClueInDetailController(String id){
        System.out.println("进入到deleteClueInDetailController处理器方法");
        System.out.println("执行在详细页删除线索的操作");

        boolean flag=clueService.deleteClueInDetailService(id);


        return flag;
    }




//线索转换
    @RequestMapping("/convert.do")
    public ModelAndView convertController(HttpServletRequest request,
                                          HttpSession session,
                                          String flag,
                                          String clueId,
                                          String money,
                                          String name,
                                          String expectDate,
                                          String stage,
                                          String activityId){
        System.out.println("进入到convertController处理器方法中");
        //System.out.println("接收到的参数:"+clueId+money+name+expectDate+stage+activityId);

        Tran tran=null;//业务实体类
        String createBy=((User)session.getAttribute("user")).getName();
        //System.out.println();


        if("true".equals(flag)){
            tran=new Tran();
            System.out.println("执行交易的创建操作奥");
            String id=UUIDUtil.getUUID();
            String createTime=DateTimeUtil.getSysTime();
            System.out.println("走到这没？？？？");
            tran.setMoney(money);
            System.out.println("那这里呢？？？");
            tran.setName(name);
            tran.setExpectedDate(expectDate);
            tran.setStage(stage);
            tran.setActivityId(activityId);
            tran.setId(id);
            tran.setCreateBy(createBy);
            tran.setCreateTime(createTime);
        }

        System.out.println("走到这里了呢");
    //为业务层传递参数,有了clueId我们才知道需要转换那条记录.tran也需要传递,因为在线索的转换过程中,有可能要创建一笔交易
        boolean flag1=clueService.convertService(clueId,tran,createBy);

            ModelAndView mv=new ModelAndView();

        if(flag1==true){
            //使用request域时我们才会使用转发
            mv.setViewName("redirect:/workbench/clue/index.jsp");
        }

            return mv;
    }





}
